package fr.mywiki.model.ejbAdapter.services;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.SessionBean;

import fr.mywiki.business.account.AccountLite;
import fr.mywiki.business.entry.EntryLite;
import fr.mywiki.business.structure.StructureLite;
import fr.mywiki.business.versioning.VersionLite;
import fr.mywiki.model.ejbAdapter.EntityHomeCache;
import fr.mywiki.model.ejbAdapter.ServicesAdapter;
import fr.mywiki.model.ejbAdapter.entity.AccountLocal;
import fr.mywiki.model.ejbAdapter.entity.EntryLocal;
import fr.mywiki.model.ejbAdapter.entity.StructureLocal;
import fr.mywiki.model.ejbAdapter.entity.VersionLocal;
import fr.mywiki.model.ejbAdapter.services.business.UpdateServiceBusiness;

/**
 * This session bean deals with adding links to bean objects.
 * 
 * @ejb.bean name = "UpdateService" 
 * 		display-name = "UpdateService"
 * 		description = "UpdateService Administration Service"
 * 		jndi-name = "ejb/MyWiki/UpdateService"
 * 		remote-business-interface = "fr.mywiki.model.ejbAdapter.services.business.UpdateServiceBusiness"
 * 
 * @ejb.home
 * 		extends = "javax.ejb.EJBHome"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 *  
 * @ejb.interface
 * 		extends = "javax.ejb.EJBObject"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 * 
 * @author tlombard
 */
public class UpdateServiceBean extends ServicesAdapter implements UpdateServiceBusiness,SessionBean {
	/**
	 * The serial version UID.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Default create method
	 * @throws CreateException
	 * @ejb.create-method 
	 */
	public void ejbCreate() throws CreateException {
	}
	
	/**
	 * Saves the attributes of the given <code>AccountLite</code> object into the database.
	 * 
	 * @param account a <code>AccountLite</code> object.
	 * @throws ObjectNotFoundException if the database object was not found.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void updateAccount(AccountLite account) throws ObjectNotFoundException{
		try {
			AccountLocal bean = EntityHomeCache.getInstance().getAccountHome()
					.findByPrimaryKey(account.getId());
			bean.setLogin(account.getLogin());
			bean.setPassword(account.getPassword());
			bean.setProfileId(account.getProfile().getId());
		} catch (FinderException e) {
			if (e.getClass() == ObjectNotFoundException.class)
				throw (ObjectNotFoundException) e;
			throw new EJBException(e);
		}
	}
	
	/**
	 * Saves the attributes of the given <code>EntryLite</code> object into the database.
	 * 
	 * @param entry a <code>EntryLite</code> object.
	 * @throws ObjectNotFoundException if the database object was not found.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void updateEntry(EntryLite entry) throws ObjectNotFoundException{
		try {
			EntryLocal bean = EntityHomeCache.getInstance().getEntryHome()
					.findByPrimaryKey(entry.getId());
			bean.setTitle(entry.getTitle());
			bean.setInternTitle(entry.getInternTitle());
			bean.setCurrentVersion(entry.getCurrentVersion());
		} catch (FinderException e) {
			if (e.getClass() == ObjectNotFoundException.class)
				throw (ObjectNotFoundException) e;
			throw new EJBException(e);
		}
	}
	
	/**
	 * Saves the attributes of the given <code>StructureLite</code> object into the database.
	 * 
	 * @param structure a <code>StructureLite</code> object.
	 * @throws ObjectNotFoundException if the database object was not found.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void updateStructure(StructureLite structure) throws ObjectNotFoundException{
		try {
			StructureLocal bean = EntityHomeCache.getInstance().getStructureHome()
					.findByPrimaryKey(structure.getId());
			bean.setName(structure.getName());
			bean.setStructureModel(structure.getStructureModel());
		} catch (FinderException e) {
			if (e.getClass() == ObjectNotFoundException.class)
				throw (ObjectNotFoundException) e;
			throw new EJBException(e);
		}
	}
	
	/**
	 * Saves the attributes of the given <code>VersionLite</code> object into the database.
	 * 
	 * @param versionId a <code>VersionLite</code> object.
	 * @throws ObjectNotFoundException if the database object was not found.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void updateVersion(VersionLite version) throws ObjectNotFoundException{
		try {
			VersionLocal bean = EntityHomeCache.getInstance().getVersionHome()
					.findByPrimaryKey(version.getId());
			bean.setDate(version.getDate());
			bean.setAuthor(version.getAuthor().toString());
			bean.setMultiversion(version.getMultiversion());
		} catch (FinderException e) {
			if (e.getClass() == ObjectNotFoundException.class)
				throw (ObjectNotFoundException) e;
			throw new EJBException(e);
		}
	}
}
